package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JTextPane;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class LoginScreen extends JFrame {

	private JFrame loginFrame;
	private JTextField usernameText;
	private JPanel loginPanel;
	private MainMenu mainMenu;
	private JPasswordField passwordText;
	private JTextPane textPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		LoginScreen loginScreen = new LoginScreen();
	}

	/**
	 * Create the application.
	 */
	public LoginScreen()
	{
		makeFrame();
		makePanel();
		loginFrame.setVisible(true);
	
	}
	
	//Creates text fields
	private void makeTextFields()
	{
		usernameText = new JTextField();
		usernameText.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				clearPane();
			}
		});
		usernameText.setBounds(111, 62, 239, 20);
		loginPanel.add(usernameText);
		usernameText.setColumns(10);
		
		passwordText = new JPasswordField();
		passwordText.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				clearPane();
				passwordText.setText("");
			}
		});
		passwordText.setBounds(111, 124, 239, 20);
		loginPanel.add(passwordText);
	}
	
	//Creates labels
	private void makeLabels()
	{
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblUsername.setBounds(10, 63, 91, 14);
		loginPanel.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblPassword.setBounds(10, 125, 91, 14);
		loginPanel.add(lblPassword);
		
		JLabel lblPleaseEnterYour = new JLabel("Please Enter Your Username And Password");
		lblPleaseEnterYour.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblPleaseEnterYour.setBounds(74, 21, 359, 14);
		loginPanel.add(lblPleaseEnterYour);
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setForeground(Color.RED);
		textPane.setBackground(SystemColor.control);
		textPane.setBounds(153, 93, 239, 20);
		loginPanel.add(textPane);
	}
	
	//Creates buttons
	private void makeButtons()
	{
		JButton buttonLogin = new JButton("Login");
		buttonLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				login();
			}
		});
		buttonLogin.setBounds(185, 161, 89, 23);
		loginPanel.add(buttonLogin);
	}
	
	//Method used for checking the username and password which, if correct, opens the main menu ui
	private void login()
	{
		String userName = usernameText.getText();
		String passWord = passwordText.getText();
		
		if(userName.equals("admin") && passWord.equals("1234"))
		{
			goToMainMenu(userName,passWord);
		}
		else{
			textPane.setText("Not A Valid Combination!!");
		}
	}
	
	//Launches the main menu UI
	private void goToMainMenu(String userName, String passWord)
	{
		mainMenu = new MainMenu(userName,passWord);
		
		loginFrame.setVisible(false);
	}
	
	//Clears the textpane
	private void clearPane()
	{
		textPane.setText("");
	}
	
	//Creates the frame
	private void makeFrame()
	{
		loginFrame = new JFrame();
		loginFrame.setBounds(100, 100, 474, 233);
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	//Creates the panels
	private void makePanel()
	{
		loginPanel = new JPanel();
		loginFrame.getContentPane().add(loginPanel, BorderLayout.CENTER);
		loginPanel.setLayout(null);
		
		makeButtons();
		makeTextFields();
		makeLabels();
	}
}